
Ext.namespace('sys.bzgsk');

	// 主页面
	sys.bzgsk.MailViewer = Ext.extend(Ext.Panel, {
		
		/**
		 * 左侧grid
		 */ 
		grid : null,
		
		/**
		 * 右侧详细信息
		 */ 
		detail : null,
		
		/**
		 * grid数据源
		 */
		storeGrid : null,
		
		/**
		 * 菜单
		 */
		commandMenu : null,
		
		border : false,
		
	    /**
	     * 
	     */
		viewerConfig : undefined,
		
		/**
	     * 初始化
		 */
		initComponent : function() {
			var obj = this;
			
			this.addListener('render', function() {
				obj.startViewer.defer(1, obj);
			});

			this.layout	= 'fit';
			sys.bzgsk.MailViewer.superclass.initComponent.call(this);
		},
		
		/**
		 * 初始化顺序处理
		 */
		startViewer : function() {
			
			// 初始化页面
			this.renderViewer();
			// grid绑定菜单
			this.menuCreate();
			// 增加拖拽事件
			//this.ddCreate();
		},
		
		/**
		 * 加载控件
		 */
		renderViewer : function() {

			var obj = this;
			var sm = new Ext.grid.CheckboxSelectionModel();
			//----------------------------------------------------------------------
			//	列表数据源
			//----------------------------------------------------------------------
			this.storeGrid = new Ext.data.Store({
				proxy :new Ext.data.HttpProxy({
					url :'bzgsk!getBzgskList.do'
				}),
				reader :new Ext.data.JsonReader({
					root :'result',
					totalProperty :'totalCount',
					fields : ['id','bzid','bzname', 'dydj','dydjname','sblx','sblxname','zymc','bzgs']
				}),
				autoLoad:true,
				remoteSort :false
			});

			//----------------------------------------------------------------------
			//	加载列表
			//----------------------------------------------------------------------
			this.grid = new Ext.grid.GridPanel({
				id:"bzgsk_grid",
				store : obj.storeGrid,
				///frame:true,
				ddGroup:"gridDDGroup",
				//enableDragDrop:true,
				stripeRows:true,
				cm: new Ext.grid.ColumnModel({
	        		defaults: {
	            		sortable: true
	        		},
	        		columns: [
	        			new Ext.grid.RowNumberer(),
	            		sm,
						{	id:'id',
							header :"序号",
							width :50,
							dataIndex :'id',
							sortable :true,
							hidden :true
						}, {
							header :"班组",
							width :50,
							dataIndex :'bzname',
							sortable :true,
							align:'center'
						}, {
							header :"电压等级",
							width :50,
							dataIndex :'dydjname',
							sortable :true,
							align:'center'
						},{
							header :"设备类型",
							width :50,
							dataIndex :'sblxname',
							sortable :true,
							align:'center'
						},{
							header :"标准作业名称",
							width :100,
							dataIndex :'zymc',
							sortable :true,
							align:'center'
						},{
							header :"标准工时",
							width :50,
							dataIndex :'bzgs',
							sortable :true,
							align:'center'
						}]
				}),
				//selModel  : new Ext.grid.RowSelectionModel({singleSelect : true}),
				sm : sm,
				loadMask : {
					msg :'载入中,请稍候...'
				},
				viewConfig:{
					forceFit:true
				},
				tbar :[
					{
		    			text : '班组：'
		    		} , {
                		xtype : 'combo',
                		width:100,
						name : 'con1',
						id:'con1',
						hiddenName : 'entity.bzid',
						store : new Ext.data.Store({
							proxy : new Ext.data.HttpProxy({
								url : 'bzgsk!getBz.do'
							}),
							reader : new Ext.data.JsonReader({}, ['id', 'organName']),
							autoLoad : true
						}),
						displayField : 'organName',
						valueField : 'id',
						forceSelection : true,
						selectOnFocus : true,
						editable : false,
						triggerAction : 'all',
						mode : 'local',
						emptyText:'请选择'
					},'-',{				       
		    			text : '电压等级：'
		    		} , {
                		xtype : 'combo',
                		width:100,
						name : 'con2',
						id:'con2',
						hiddenName : 'entity.dydj',
						store : new Ext.data.Store({
							proxy : new Ext.data.HttpProxy({
								url : 'dic!getDicJson.do?dic.itemType=DYDJ&emptyFlag=1'
							}),
							reader : new Ext.data.JsonReader({}, ['itemId', 'itemName']),
							autoLoad : true
						}),
						displayField : 'itemName',
						valueField : 'itemId',
						forceSelection : true,
						selectOnFocus : true,
						editable : false,
						triggerAction : 'all',
						mode : 'local',
						emptyText:'请选择'
					},'-',{
		    			text : '设备类型：'
		    		} , {
                		xtype : 'combo',
                		width:100,
						name : 'con3',
						id:'con3',
						hiddenName : 'entity.sblx',
						store : new Ext.data.Store({
							proxy : new Ext.data.HttpProxy({
								url : 'dic!getDicJson.do?dic.itemType=SBLX'
							}),
							reader : new Ext.data.JsonReader({}, ['itemId', 'itemName']),
							autoLoad : true
						}),
						displayField : 'itemName',
						valueField : 'itemId',
						forceSelection : true,
						selectOnFocus : true,
						editable : false,
						triggerAction : 'all',
						mode : 'local',
						emptyText:'请选择'
		    		} , '-',{
						id :'noteList_query',
						text :'查询',
						cls:'x-btn-text-icon',
        				icon:contextpath + '/images/page_find.png',
			            handler:function() {
			            	obj.storeGrid.baseParams['searchBean.con1'] = Ext.getCmp('con1').value;
			            	obj.storeGrid.baseParams['searchBean.con2'] = Ext.getCmp('con2').value;
			            	obj.storeGrid.baseParams['searchBean.con3'] = Ext.getCmp('con3').value;
			            	obj.storeGrid.reload({params:{start:0,limit:30}});
        				}
					}, btn_bzgsk_delete ? '' : '-',{
						id :'bzgsk_delete',
						text :'确定',
						cls:'x-btn-text-icon',
        				icon:contextpath + '/images/save2.gif',
        				hidden:false,
        				handler:function() {
        					obj.refresh();
        				}
					}/**, btn_bzgsk_delete ? '' : '-',{
						id :'bzgsk_delete',
						text :'删除',
						cls:'x-btn-text-icon',
        				icon:contextpath + '/images/delete.gif',
        				hidden:btn_bzgsk_delete,
        				handler:function() {
        					obj.deleteBzgsk();
        				}
					}*/
				],
				bbar : new Ext.PagingToolbar({
					store : obj.storeGrid,
					pageSize:30,
					displayInfo :true,
					displayMsg :'共 {2} 条',
					emptyMsg :"没有数据显示！"
				}),
				listeners:{'rowdblclick':function(row) {
					obj.getDetail();
				}}
			});
			
			//----------------------------------------------------------------------
			//	加载详细信息
			//----------------------------------------------------------------------
			this.detail = new Ext.form.FormPanel({
				id : 'bzgsk_detail',
				//frame:true,
				bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
				//autoScroll:true,
				//containerScroll:true,
				method :'POST',
				labelAlign:'right',
				tbar :[
					{
						id :'bzgsk_insert',
						text :'新建',
						cls:'x-btn-text-icon',
        				icon:contextpath + '/images/add.gif',
			            //hidden:btn_bzgsk_insert,
        				handler:function() {
        					obj.detail.getForm().reset();
        				}
					}, btn_bzgsk_save ? '' : '-',{
						text:'保存',
						icon:contextpath + "/images/save2.gif",
						//hidden:btn_bzgsk_save,
						handler: function() {
        					obj.submit();
        				}
					}
				],
		        items: [
	        	{
					fieldLabel :'班组',
					name :'bzid',
					id : 'bzid',
					xtype : 'combo',
					width:150,
					hiddenName : 'entity.bzid',
					store : new Ext.data.Store({
						proxy : new Ext.data.HttpProxy({
							url : 'bzgsk!getBz.do'
						}),
						reader : new Ext.data.JsonReader({}, ['id', 'organName']),
						autoLoad : true,
						listeners:{'load' : function() {
								var r = new Ext.data.Record({id:'', organName:''});
								Ext.getCmp('bzid').store.insert(0,r);
							
						}}
					}),
					displayField : 'organName',
					valueField : 'id',
					forceSelection : true,
					selectOnFocus : true,
					editable : false,
					triggerAction : 'all',
					mode : 'local',
					allowBlank : false,
					emptyText:'请选择'
        		},  {
						fieldLabel :'电压等级',
						name :'dydj',
						id : 'dydj',
						xtype : 'combo',
						width:150,
						hiddenName : 'entity.dydj',
						store : new Ext.data.Store({
							proxy : new Ext.data.HttpProxy({
								url : 'dic!getDicJson.do?dic.itemType=DYDJ'
							}),
							reader : new Ext.data.JsonReader({}, ['itemId', 'itemName']),
							autoLoad : true,
							listeners:{'load' : function() {
									var r = new Ext.data.Record({itemId:'', itemName:''});
									Ext.getCmp('dydj').store.insert(0,r);
								
							}}
						}),
						displayField : 'itemName',
						valueField : 'itemId',
						forceSelection : true,
						selectOnFocus : true,
						editable : false,
						triggerAction : 'all',
						mode : 'local',
						allowBlank : false,
						emptyText:'请选择'
        		},{
						fieldLabel :'设备类型',
						name :'sblx',
						id : 'sblx',
						xtype : 'combo',
						width:150,
						hiddenName : 'entity.sblx',
						store : new Ext.data.Store({
							proxy : new Ext.data.HttpProxy({
								url : 'dic!getDicJson.do?dic.itemType=SBLX'
							}),
							reader : new Ext.data.JsonReader({}, ['itemId', 'itemName']),
							autoLoad : true,
							listeners:{'load' : function() {
									var r = new Ext.data.Record({itemId:'', itemName:''});
									Ext.getCmp('sblx').store.insert(0,r);
								
							}}
						}),
						displayField : 'itemName',
						valueField : 'itemId',
						forceSelection : true,
						selectOnFocus : true,
						editable : false,
						triggerAction : 'all',
						mode : 'local',
						allowBlank : false,
						emptyText:'请选择'        			
        		},{
            			xtype: 'textarea',
            			height:20,
            			name: 'entity.zymc',
            			id : 'entity.zymc',
            			fieldLabel: '标准作业名称',
           				maxLength:100,
           				anchor: '80%',
            			allowBlank:true,
						maxLength :100
        		},{
						xtype : 'textfield',
						fieldLabel :'标准工时',
        				width: 150,
            			name: 'entity.bzgs',
            			id : 'entity.bzgs',
						allowBlank : false,
						maxLength :50       		       		
        		},{
            			xtype: 'hidden',
            			name: 'entity.id',
            			id:'entity.id',
            			value:'0'
        		}]/*,
        		buttonAlign:'right',
        		buttons:[{y:300,text:'新建',id:'btnSubmit',hidden:btn_bdz_insert, handler:function(){obj.detail.getForm().reset();}},
						 {text:'保存',id:'btnSave',  hidden:btn_bdz_save,   handler:function(){obj.submit()}}]*/
			});

			var viewer = new Ext.Panel({
			        layout:'border',
			        items:[{
			        	title:'标准工时库信息列表',
			            region:'center',
			            layout:'fit',
			            width:800,
			            items:[obj.grid],
			            split:true,
			            collapsible:true,
			            //collapseMode: 'mini',
			            defaults:{autoScroll:true,border:false},
			            layoutConfig:{
			                animate:true
			            }
			        }/**,{
			        	title:'标准工时库基本信息',
			            region:'center',
			            layout:'fit',
			            items:[obj.detail]
			        }*/]
			});
	
			this.add(viewer);

			if ((typeof this.layout) == "object") this.doLayout();

		},
		
		/**
		 * 菜单绑定
		 */
		menuCreate: function (){

			var obj = this;

			var actEdit = new Ext.Action({
				id			: 'menu_edit',
				text		: '修改',
				tooltip		: '修改当前变电站',
				iconCls		: 'icon_edit',
				handler		: function() { obj.getDetail()}
			});

			var actDelete = new Ext.Action({
				id			: 'menu_delete',
				text		: '删除',
				tooltip		: '删除当前变电站',
				iconCls		: 'icon_delete',
				hidden      : btn_bzgsk_delete,
				handler		: function() { obj.deleteBzgsk()}
			});
			
			obj.commandMenu = new Ext.menu.Menu({
				minWidth	: 160,
				items	: [
					actEdit, '-',
					actDelete
				]
			});
			
			obj.grid.on('rowcontextmenu',function (grid, rowIndex, e) {
      			e.preventDefault();
      			obj.grid.getSelectionModel().selectRow(rowIndex);
        		obj.commandMenu.showAt(e.getXY());   
			});
			
		},
		
		/**
		 * 拖拽
		 */
		ddCreate: function () {
			
			var obj = this;
			
			var formPanelDropTarget = new Ext.dd.DropTarget(obj.detail.body.dom, {
				ddGroup     : 'gridDDGroup',
				notifyEnter : function(ddSource, e, data) {
					obj.detail.body.stopFx();
					obj.detail.body.highlight();
				},
				notifyDrop  : function(ddSource, e, data){
					
					var record = ddSource.dragData.selections[0]['data'];
					
					for (var p in record) {
						try {
							obj.detail.getForm().findField("entity." + p).setValue(record[p]);
						} catch (e) {
							obj.detail.getForm().findField("entity.dydj").setRawValue(record[p]);
						}
					}
					return(true);
				}
			});
			
		},
           refresh: function() {
		   var obj = this;
			var ids = '';
			if (!obj.grid.getSelectionModel().hasSelection()) {
				Ext.MessageBox.alert("提示","请先选择标准作业工序");
				return;
			}
			
			var records = obj.grid.getSelectionModel().getSelections();
			for(var i=0;i<records.length;i++){
				record = records[i]['data'];
				ids += record['id'] + ',';
			}
			
			if(ids != ''){
				ids = ids.substring(0,ids.length-1);
			}
			//alert(ids);
			window.opener.refresh(ids);
			window.close();
		},
		/**
		 * 提交保存
		 */
		submit: function() {
			
			var obj = this;
			
			// 验证有效性
        	if (!obj.detail.getForm().isValid()) {
        		return;
        	} 

			// 提交
			obj.detail.getForm().submit({
				waitMsg :'保存中...',
				url:'bzgsk!save.do',
				success : function(form, action) {
					Ext.MessageBox.alert('提示', action.result.msg);
					if (Ext.isEmpty(action.result.rep)) {
						obj.detail.getForm().findField("entity.id").setValue(action.result.id);
						obj.storeGrid.reload({params:{start:0,limit:30}});
					}
				},
				failure : function(action) {
					Ext.MessageBox.alert('提示', '保存失败。');
				}
			});
			
		},
		
		/**
		 * 删除
		 */
		deleteBzgsk: function() {
			
			var obj = this;
			
			if (!obj.grid.getSelectionModel().hasSelection()) {
				Ext.MessageBox.alert("提示","请先选择要删除的记录");
				return;
			}
			
			var ids = obj.grid.getSelectionModel().getSelected()['data']['id'];
			
			Ext.MessageBox.confirm('提示', '确定要删除选中的【' + obj.grid.getSelectionModel().getSelected()['data']['zymc'] + '】记录吗？', function (btn) {
				
				if (btn == 'yes') {

					Ext.Ajax.request({
						method :'POST',
						url :'bzgsk!delete.do',
						params : {
							id :ids
						},
						success : function(response) {
							Ext.MessageBox.alert('提示', response.responseText);
							obj.grid.store.reload();
						},
						failure : function() {
							Ext.MessageBox.alert('提示', '删除失败！');
						}
					});
				}

			});
			
		},
		
		/**
		 * 获得详细信息
		 */
		getDetail: function() {

			var obj = this;
			
			var record = obj.grid.getSelectionModel().getSelected()['data'];
			
			for (var p in record) {
				try {
					obj.detail.getForm().findField("entity." + p).setValue(record[p]);
				} catch (e) {
					//obj.detail.getForm().findField("entity.bzid").setRawValue(record[p]);
				}
			}
            this.detail.getForm().findField("entity.dydj").setRawValue(record['dydjname']);
			this.detail.getForm().findField("entity.sblx").setRawValue(record['sblxname']);
			this.detail.getForm().findField("entity.bzid").setRawValue(record['bzname']);
		}

	});
		